// pages/index/index-main/main.js
var config = require('../../../utils/config.js')
var utils = require('../../../utils/util.js')
var QQMapWX = require('../../../lib/qqmap-wx-jssdk.js')
var qqmapssdk = new QQMapWX({
  key: 'XEFBZ-BFQKG-QCXQM-IUH47-AZMI7-BHBIP'
})
import { Target, HttpUtil } from '../../../utils/http_util.js'

var order = ['red', 'yellow', 'blue', 'green', 'red']

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrlBase: config.ictx,
    swiperList: [ //首页轮播图信息
      {
        url: config.ictx +"images/small/wx31d07722f7472e7d.o6zAJswQ66c4SNkoeIi17rEZOz6g.U1J6M32iDtqZ888552dc421e30f4cd3de49895260a08.jpeg",
        gourl:""
      },
      {
        url: config.ictx + "images/small/5ad54dbd2a4d4.jpg",
        gourl: ""
      }
    ],
    interval: '3000', //滚动停留时间
    locationInfo: '',
    menuList: [{ menu_name:"足疗",
      menu_logo: 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync("/images/menu_zuliao.png", 'base64'), 
      }, {
      menu_name: "推拿",
        menu_logo: 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync("/images/menu_tuina.png", 'base64'), 
      },{
        menu_name: "男士SPA",
        menu_logo: 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync("/images/menu_spa.png", 'base64'), 
      },{
        menu_name: "美容护肤",
        menu_logo: 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync("/images/menu_hufu.png", 'base64'), 
      } , {
        menu_name: "美妆",
        menu_logo: 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync("/images/menu_meizhuang.png", 'base64'), 
      }, {
        menu_name: "美甲",
        menu_logo: 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync("/images/menu_meijia.png", 'base64'), 
      }, {
        menu_name: "拔罐",
        menu_logo: 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync("/images/menu_baguan.png", 'base64'), 
      }, {
        menu_name: "体型",
        menu_logo: 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync("/images/menu_meiti.png", 'base64'), 
      }
    ],
    isShows:false,  //判断分类显示
    // 西安市区
    areaLise: [
      { id: '', name: '不限' },
      { id: 1, name: '推拿' },
      { id: 2, name: '美容护肤' },
      { id: 3, name: '美妆' },
      { id: 4, name: '美甲' },
      { id: 5, name: '拔罐' },
      { id: 6, name: '体型' },
      { id: 7, name: '足疗' }
    ],
    formdata:{
      classType:'', //0默认分类不限
      classNmae:'不限',
      distance:false,  //false默认不查询
      evaluation:false
    },
    datalist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that  = this;
    var param = {
      viewProfessionalTitle: that.data.formdata.classType,
      distance: that.data.formdata.distance,
      evaluation: that.data.formdata.evaluation
    }
    that.loadData(param);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  swiperDetail: function (event) {
    //console.log(event);
    //var url = event.currentTarget.dataset.url;
    wx.navigateTo({
      url: '/pages/index/list/list?type='+1,
    })
  },
  pageto: function (event){
    console.log(event)
    wx.navigateTo({
      url: '/pages/index/list/list?type=' + 1,
    })
  },
  goDtail:function(event){
    var id = event.currentTarget.dataset.id;  
    wx.navigateTo({
      url: '/pages/index/detail/detail?id='+id,
    })
  },
  goAddressManage:function(event){
    wx.navigateTo({
      url: "/pages/user/addressManage/addressManage",
    })
  },
  
  // 筛选导航栏事件
  menuClick: function (e) {
    var that =this;
    that.setData({
      isShows:true  
    })
  },
  searcheva:function(){
    var that =this;
    if (that.data.formdata.evaluation){
      that.setData({
        'formdata.evaluation': false,
      });
      var param = {
        viewProfessionalTitle: that.data.formdata.classType,
        distance: that.data.formdata.distance,
        evaluation: false
      }
      that.loadData(param);
    }else{
      that.setData({
        'formdata.evaluation': true,
        'formdata.distance': false,
      }); 
      var param = {
        viewProfessionalTitle: that.data.formdata.classType,
        distance: false,
        evaluation: true
      }
      that.loadData(param);
    }
  },
  searchdis:function() {
    var that = this;
    if (that.data.formdata.distance) {
      that.setData({
        'formdata.distance': false,
      });
      var param = {
        viewProfessionalTitle: that.data.formdata.classType,
        distance: false,
        evaluation: that.data.formdata.evaluation
      }
      that.loadData(param);
    }else{
      that.setData({
        'formdata.evaluation': false,
        'formdata.distance': true,
      });
      var param = {
        viewProfessionalTitle: that.data.formdata.classType,
        distance: true,
        evaluation: false
      }
      that.loadData(param);
    }
  },  
  getLocation:function(e){
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function(result){
        console.log('location', result);
        var locationString = result.latitude + "," + result.longitude;
        //初始化地址 加载列表数据
        var param = {
          viewProfessionalTitle: that.data.formdata.classType,
          distance: that.data.formdata.distance,
          evaluation: that.data.formdata.evaluation
        }
        param.consumerLongitude = result.longitude;
        param.consumerLatitude = result.latitude;
        HttpUtil.get(Target.self, "artificerMeans/searchList", param).then(res => {
          console.log(res);
          that.setData({
            datalist: res.list
          });
        })

        qqmapssdk.reverseGeocoder({
          location: locationString,
          success: function(res){
            console.log(res); 
            getApp().globalData.locationInfo = res.result.address;
            that.setData({
              locationInfo: res.result.address
            })
            try{
              var locationInfo={
                lid:0,  //0代表当前位置
                latitude: result.latitude,
                longitude: result.longitude,
                addr: res.result.address
              }
              wx.setStorageSync('locationInfo', locationInfo )
              
            }catch(e){
              console.log(e)
            }
          }
        })
      }
    })

  },
  classresearch:function(event){
    var that = this;
    var item = event.currentTarget.dataset.classty;  
    console.log(item);   
    that.setData({
      "formdata.classType":item.id,
      "formdata.classNmae": item.name,
      isShows:false
    })
    var param = {
      viewProfessionalTitle: item.id,
      distance: that.data.formdata.distance,
      evaluation: that.data.formdata.evaluation
    }
    that.loadData(param)
  },
  //加载技师数据
  loadData:function(param){
    var that =this;
    wx.getStorage({
      key: 'locationInfo',
      success(lores) {
        if (lores === null || lores.data === null || lores.data.lid === null || lores.data.lid===''){
          that.getLocation();
        }else{
          param.consumerLongitude = lores.data.longitude;
          param.consumerLatitude = lores.data.latitude;
          HttpUtil.get(Target.self, "artificerMeans/searchList", param).then(res => {
            that.setData({
              datalist:res.list
            });
          })
          that.setData({
            locationInfo: lores.data.addr
          });
          
        }
        
      },
      fail(){
        that.getLocation();
      }
    })
  }

})